﻿/* 
 *  <copyright file="InputToMenuEvent.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using SobrietyEngine.Events;
using SobrietyEngine.Events.Input;
using SobrietyEngine.Events.Menu;
using SobrietyEngine.Guids;
using SobrietyEngine.Screens;

namespace SobrietyEngine.Input.Mapping
{
    public class InputToMenuEvent:BaseMapper
    {

        public InputToMenuEvent(IEventRouter eventRouter, CGuid owner):base(eventRouter, owner)
        {
        }

        public override void RegisterEventListeners()
        {
            EventRouter.AddEventListener(typeof(JoystickMove), this);
            EventRouter.AddEventListener(typeof(ButtonPress), this);
            EventRouter.AddEventListener(typeof(Trigger), this);
            EventRouter.AddEventListener(typeof(KeyPress), this);
            Enabled = true;
        }

        public override void UnregisterEventListeners()
        {
            EventRouter.RemoveEventListener(this);
            Enabled = false;
        }

        //joystick / keyboard input events come in, menu events go out
        public override void HandleEvent(Event e)
        {
            if (!IsInputMine(e))
                return;

            IInputEvent ie = (IInputEvent)e.Data;
            if (ie.InputDevice.GetType() == typeof(Input.Devices.GamepadDevice))
            {
                if(e.Type == typeof(ButtonPress))
                {
                    ButtonPress bp = (ButtonPress)e.Data;
                    ScreenActionsEnum.Actions sa = ScreenActionsEnum.Actions.None;
                    //A = select
                    //B = back
                    //Dpad = navigate
                    if (bp.Button == GamepadEnums.GamepadButton.A)
                        sa = ScreenActionsEnum.Actions.Select;
                    if (bp.Button == GamepadEnums.GamepadButton.B)
                        sa = ScreenActionsEnum.Actions.Back;
                    if (bp.Button == GamepadEnums.GamepadButton.DU)
                        sa = ScreenActionsEnum.Actions.Up;
                    if (bp.Button == GamepadEnums.GamepadButton.DD)
                        sa = ScreenActionsEnum.Actions.Down;
                    if (bp.Button == GamepadEnums.GamepadButton.DL)
                        sa = ScreenActionsEnum.Actions.Left;
                    if (bp.Button == GamepadEnums.GamepadButton.DR)
                        sa = ScreenActionsEnum.Actions.Right;

                    //raise our menu event
                    if(sa != ScreenActionsEnum.Actions.None)
                        EventRouter.RaiseEvent(this, new MenuAction(sa));
                    return;
                }                   
            }
        }
    }
}
